<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2020/8/30
 * Time: 17:19
 */

use app\components\ViewHtmlAuto;
use yii\helpers\Html;

///var_dump($this->context);


$Model = $this->context->getModel();
// var_dump($Model);exit;
    if(!$Model){
        \app\components\Fun::showmsg("没有数据");
    }
$attributeLabels = $Model->attributeLabels();


/**
 * 获取字段的标题
 */
$getTitle = function ($key, $vo) use ($attributeLabels) {
    $title = $vo['title'] ?? '';
    if ($title !== '') {
        return $title;
    }
    return $attributeLabels[$key] ?? '';
};
///文本输入框
function viewText($title, $name, $value)
{
    $lihtml = ' <li class="li">
                    <p class="title">' . $title . '</p>
                    <input type="text" placeholder="请输入' . $title . '"
                           name="' . $name . '" value="' . $value . '"
                           class="mui-input"/>
                </li>';
    return $lihtml;
}

///性别选择
function viewSex($title, $name, $value)
{
    $lihtml = '<li class="li">
                    <p class="title">' . $title . '</p>
                    ' . Html::dropDownList($name, $value, [
            "" => '请选择性别',
            \app\models\BaseUserModel::Sex_Nan => '男',
            \app\models\BaseUserModel::Sex_Nv => '女',
            \app\models\BaseUserModel::Sex_buxian => '不限',
        ]) . '</li>';
    return $lihtml;
}

///多行输入框
function viewTextarea($title, $name, $value)
{
    $lihtml = ' <li class="li">
                    <p class="title">' . $title . '</p>
                    <textarea type="text" placeholder="请输入' . $title . '"
                           name="' . $name . '" 
                           class="mui-input">' . $value . '</textarea>
                </li>';
    return $lihtml;
}


    /**
     * 基本信息的标签链接
    */
    function viewBaseLableA($title, $key, $value, $vo,$id){
        $href=$vo['href']??'';
        $href.=$id;
        if($value>0){
            $value="已经上传";
        }else{
            $value="没有上传";
        }
        $lihtml = ' <li class="li">
                    <p class="title">' . $title . '</p>
                    <a href="'.$href.'" class="mui-input">' . $value . '</a>
                </li>';
        return $lihtml;
    }

$vLi = function ($key, $vo) use ($getTitle, $Model) {
    $title = $getTitle($key, $vo);
    $value = $Model[$key];
    $view=$vo['view']??ViewHtmlAuto::viewText;
    if ($view == ViewHtmlAuto::viewText) {
        return viewText($title, $key, $value);
    }
    if ($view == ViewHtmlAuto::viewSex) {
        return viewSex($title, $key, $value);
    }
    if ($view == ViewHtmlAuto::viewTextarea) {
        return viewTextarea($title, $key, $value);
    }
    if($view==ViewHtmlAuto::viewBaseLableA){
        return viewBaseLableA($title, $key, $value, $vo,$Model[$this->context->setId]);
    }
    if($view==ViewHtmlAuto::viewPic){
        return viewText($title, $key, $value);
    }
}


?>
<div class="mui-content-padded H5body basedata">
    <div class="content" action="<?= Yii::$app->request->url ?>">

        <ul class="baseul">
            <?php
            $list = $this->context->fieldList;
            foreach ($list as $key => $vo) {
                echo $vLi($key, $vo);
            }
            ?>
        </ul>
        <?php if(!$this->context->readOnly){ ?>
        <button class="savebtn btn  btn-info btn-sm">保存</button>
        <?php } ?>
    </div>
    <script>
        var content = $(".H5body .content");
        content.find('.savebtn').on("click", function () {
            var action = content.attr("action");
            var objlistTag = content.find('[name]');
            var datalist = {};
            for (var i = 0; i < objlistTag.length; i++) {
                var one = $(objlistTag[i]);
                datalist[one.attr('name')] = one.val();
            }
            sys.post(action, datalist, function (data) {
                console.log(data);
                m.tishi(data.data);
            });

        });

    </script>
</div>
